package com.nitty.cash.loop.sdk.analyse.util

import com.lib.common.http.OkHttp
import com.lib.common.http.callback.StringCallback
import com.lib.common.http.model.Response
import com.nitty.cash.loop.sdk.analyse.AnalyseBuilder
import com.nitty.cash.loop.utils.aes.NittyAesUtil
import com.nitty.cash.loop.utils.log.JsonFormat
import com.nitty.cash.loop.utils.log.NittyLogger


object UrlHelper {

    fun combineUrl(): String {
        if (AnalyseBuilder.host.isBlank() or
            AnalyseBuilder.zone.isBlank() or
            AnalyseBuilder.pathCommon.isBlank()
        ) {
            return ""
        }

        val zone = AnalyseBuilder.zone
        AnalyseBuilder.zone = zone.replace("/", "")

        val pathCommon = AnalyseBuilder.pathCommon
        AnalyseBuilder.pathCommon = if (pathCommon.startsWith("/")) {
            pathCommon.replace("/", "")
        } else {
            pathCommon
        }

        return AnalyseBuilder.host + "/" + AnalyseBuilder.zone + "/" + AnalyseBuilder.pathCommon
    }
}

object AnalyseRequestUtil {
    fun doPostAction(toUrl: String, body: String) {
        if (toUrl.isBlank()) return
        if (body.isBlank()) return

        NittyLogger.i("Analyse Url：  $toUrl")
        NittyLogger.i("Analyse Params： $body")

        OkHttp.post<String>(toUrl)
            .tag(this)
            .upJson(body)
            .execute(object : StringCallback() {
                override fun onSuccess(response: Response<String>) {
                    val result = NittyAesUtil.decryptDefault(response.body())
                    Thread { NittyLogger.i("Analyse Response：${JsonFormat.format(result)}") }.start()
                }
                override fun onError(response: Response<String>) {
                }
                override fun onFinish() {
                }
            })
    }
}


